# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
import MySQLdb
import settings

class ReviewsPipeline(object):
    def __init__(self):
        # 连接数据库
        self.connect = MySQLdb.connect(
            host=settings.MYSQL_HOST,
            db=settings.MYSQL_DBNAME,
            user=settings.MYSQL_USER,
            passwd=settings.MYSQL_PASSWD,
            charset=settings.MYSQL_CHARSET,
            use_unicode=True)

        # 通过cursor执行增删查改
        self.cursor = self.connect.cursor()

    def process_item(self, item, spider):
        try:
            params = [item['href'],item['title'],item['des'],item['author'],item['time'],item['thumb']]
            sql = "insert into article(href,title,des,author,time,thumb) VALUES (%s,%s,%s,%s,%s,%s)"
            res = self.cursor.execute(sql,params)
            print(res)
            self.connect.commit() #必写
        except Exception as e:
            print(e)

        return item

    def spider_close(self):
        self.cursor.close()
        self.connect.close()
